<?php

namespace App\Http\Controllers\home\user;


use App\Models\Coupon;
use App\Models\Temp_phone;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\User;
use App\Models\User_info;
use App\Models\Doctor;
use App\Models\District;
use App\Models\Address;
use App\Models\Cart;

class AjaxController extends Controller
{
    //用户登录验证
    public function ylogin(Request $request)
    {
        $messages = [
            'username.required'=>'手机号不能为空',
            'username.regex'=>'手机号错误',
            'password.required'=>'密码不能为空',
            'password.regex'=>'密码必须是6-15位的数字，字母',
        ];
        $this->validate($request, [
            'username' => 'bail|required|regex:/^1[34578]\d{9}$/',
            'password' => 'bail|required|regex:/^[0-9a-zA-Z_]{6,15}$/',
        ],$messages);
        $user = new User();
        $ob = $user->checkLogin($request);
        if ($ob) {
            if ($ob->status == 2) {
                return json_encode("账号被冻结请联系管理员：zxz!");
            } else {
                $ob->ip=$_SERVER['REMOTE_ADDR'];
                $ob->ltime=date('Y-m-d H:i:s',time());
                $ob->save();
                session(['homeuser' => $ob->id]);
                return json_encode(true);
            }
        } else {
            return json_encode("账号不存在或密码错误!");
        }
    }

    //医师登录验证
    public function doclogin(Request $request)
    {
        $doctor = new Doctor();
        $ob = $doctor->checkLogin($request);
        if ($ob) {
            session(['docuser' => $ob]);
            session()->forget('homeuser');
            session()->forget('sender');
            return json_encode(true);
        } else {
            return json_encode("账号不存在或密码错误!");
        }
    }

    //判断账号是否注册
    public function resgName(Request $request)
    {
        $name = $request->input('name');
        $val = $request->input('value');
        if ($name == 'username') $name = "手机号";
        if ($name == 'password') $name = "密码";
        switch ($name) {
            case '手机号':
                $user = new User();
                $m = $user->checkName($val);
                if ($m) {
                    $list = $name . "已经注册！";
                    return json_encode($list);
                }
                break;
        }
        return json_encode(1);
    }

    //注册
    public function regsuc(Request $request)
    {
        $messages = [
            'username.required'=>'手机号不能为空',
            'username.regex'=>'手机号错误',
            'password.required'=>'密码不能为空',
            'password.regex'=>'密码必须是6-15位的数字，字母',
        ];
        $this->validate($request, [
            'username' => 'bail|required|regex:/^1[34578]\d{9}$/',
            'password' => 'bail|required|regex:/^[0-9a-zA-Z_]{6,15}$/',
        ],$messages);
        if($request->input('password') != $request->input('pwd')){
            return json_encode("两次密码不一致！");
        }
        $phone = $request->input('phone');
        $pid = $request->input('pid','0');
        $time = time();
        $temp = Temp_phone::where('phone', $phone)->where('deadline', '>', $time)->orderBy('deadline', 'desc')->first();
        if ($temp) {
            $user = new User();
            $user->username = $request->input('username');
            $user->password = md5($request->input('password'));
            $user->save();
            if($pid!=''){
                $user->pid=$pid;
                //给推荐人发优惠券
                $coupon=new Coupon();
                $coupon->user_id=$pid;
                $coupon->value=10;
                $coupon->status=1;
                $coupon->info='满40元可用';
                $coupon->start=date(time());
                $coupon->end=date(time()+2592000);
                $coupon->threshold=40;
                $user->save();
                $coupon->save();
                //给注册者发优惠券
                $coupon_u=new Coupon();
                $coupon_u->user_id=$user->id;
                $coupon_u->value=10;
                $coupon_u->status=1;
                $coupon_u->info='满40元可用';
                $coupon_u->start=date(time());
                $coupon_u->end=date(time()+2592000);
                $coupon_u->threshold=40;
                $coupon_u->save();
            }
            if ($user->id > 0) {
                $user_info = new User_info();
                $user_info->user_id = $user->id;
                $user_info->phone = $request->input('username');
                $user_info->nickname = $request->input('username');
                $info_id = $user_info->save();
                if ($info_id) {
                    session(['homeuser' => $user->id]);

                    return json_encode('账号注册成功!');
                } else {
                    return json_encode("账号存在问题！");
                }
            } else {
                return json_encode("账号存在问题！");
            }
//        } else {
//            return json_encode("验证码错误或已失效");
        }

    }

    //修改密码
    public function pwd(Request $request)
    {
        $id = session('homeuser');
        $user = new User();
        $ob = $user->updatepwd($request, $id);
        if ($ob) {
            return json_encode("密码修改成功!");
        } else {
            return json_encode("原密码不正确！");
        }
    }

    //获取省份
    public function docity(Request $request)
    {
        $district = new District();
        $list = $district->dissel($request);
        return json_encode($list);
    }

    //获取城市
    public function doxian(Request $request)
    {
        $district = new District();
        $list = $district->dissel($request);
        return json_encode($list);
    }

    //添加用户送货地址
    public function adddizhi(Request $request)
    {
        $messages = [
            'phone.required'=>'手机号不能为空',
            'phone.regex'=>'手机号错误',
        ];
        $this->validate($request, [
            'phone' => 'bail|required|regex:/^1[34578]\d{9}$/',
        ],$messages);
        $this->changesta();
        $address = new Address();
        $address->user_id = session('homeuser');
        $address->name = $request->input('name');
        $address->phone = $request->input('phone');
        $address->detail = $request->input('detail');
        $address->city = $request->input('dizhi');
        $address->status = 2;
        $bo = $address->save();
        if ($bo) {
            return json_encode('地址添加成功!');
        } else {
            return json_encode('地址添加失败!');
        }
    }

    public function changesta()
    {
        $user_id = session('homeuser');
        Address::where('user_id', $user_id)->where('status', '2')->update(['status' => 1]);
    }

    //编辑地址
    public function changedizhi(Request $request)
    {
        $this->changesta();
        $id = $request->input('id');
        $address = Address::find($id);
        $address->name = $request->input('name');
        $address->phone = $request->input('phone');
        $address->detail = $request->input('detail');
        $address->city = $request->input('dizhi');
        $address->status = 2;
        $bo = $address->save();
        if ($bo) {
            return json_encode('地址编辑成功!');
        } else {
            return json_encode('地址添加失败!');
        }
    }

    //修改默认地址
    public function defdizhi(Request $request)
    {
        $id = $request->input('id');
        $this->changesta();
        $address = Address::find($id);
        $address->status = 2;
        $bo = $address->save();
        if ($bo) {
            return json_encode(true);
        } else {
            return json_encode(false);
        }
    }

    //查询底部导航栏的购物车商品数量
    public function num()
    {
        if(session()->has('homeuser')) {
        $cart = new Cart();
        $bo= $cart->donum();
        if($bo){
            $num = $bo;
        }else{
            $num = 0;
        }
    }else{
        $num = 0;
    }
        return json_encode($num);
    }

    //查找购物车信息
    public function selcart()
    {
        $cart = new Cart();
        $ob = $cart->sel();

        return json_encode($ob);



    }

    //商品的选中状态
    public function cartStatus(Request $request)
    {
        $id = $request->input('id');
        $cart = Cart::find($id);
        $cart->status = $request->input('status');
        $bo = $cart->save();
        if($bo){
            $cartSum = new Cart();
            $cart->sum = $cartSum->sumCart();
            return json_encode($cart);
        }else{
            return json_encode(false);
        }
    }

    //购物车商品加1
    public function doadd(Request $request)
    {
        $id = $request->input('id');
        $carts = new Cart();
        $row = $carts->selfirst($request);
        if ($row) {
            if($row->count == $row->stock){
                return json_encode(false);
            }
            $cart = Cart::find($id);
            $cart->count = $row->count + 1;
            $bo = $cart->save();
            if ($bo) {
                $cartSum = new Cart();
                $cart->sum = $cartSum->sumCart();
                $cartNum = new Cart();
                $cart->num = $cartNum->donum();
                return json_encode($cart);
            } else {
                return json_encode(false);
            }
        } else {
            return json_encode(null);
        }


    }

    //购物车商品减1
    public function dominus(Request $request)
    {
        $id = $request->input('id');
        $carts = new Cart();
        $row = $carts->selfirst($request);
        if ($row) {
            if ($row->count - 1 == 0) {
                $delcart = new Cart();
                $bo = $delcart->delfirst($request);
                if ($bo) {
                    $cartSum = new Cart();
                    $row->sum = $cartSum->sumCart();
                    $cartNum = new Cart();
                    $row->num = $cartNum->donum();
                    return json_encode($row);
                } else {
                    return json_encode(false);
                }
            } else {
                $cart = Cart::find($id);
                $cart->count = $row->count - 1;
                $bo = $cart->save();
                if ($bo) {
                    $cartSum = new Cart();
                    $cart->sum = $cartSum->sumCart();
                    $cartNum = new Cart();
                    $cart->num = $cartNum->donum();
                    $cart->delete = 2;
                    return json_encode($cart);
                } else {
                    return json_encode(false);
                }
            }
        } else {
            return json_encode(false);
        }
    }

    public function delProduct(Request $request)
    {
        $carts = new Cart();
        $row = $carts->selfirst($request);
        if ($row) {
            $delcart = new Cart();
            $bo = $delcart->delfirst($request);
            if ($bo) {
                $cartSum = new Cart();
                $row->sum = $cartSum->sumCart();
                $cartNum = new Cart();
                $row->num = $cartNum->donum();
                return json_encode($row);
            } else {
                return json_encode(false);
            }
        }
    }

    //清空购物车
    public function delqing()
    {
        $cart = new Cart();
        $bo = $cart->doqing();
        if ($bo) {
            return json_encode(true);
        } else {
            return json_encode(false);
        }
    }

    //将商品加入购物车
    public function incart(Request $request)
    {
        $ob=new Cart();
        $ob->sel();
        $pid = $request->input('pid');
        $uid = session('homeuser');
        $obb = Cart::where('product_id', $pid)->where('user_id',$uid);
        $row = $obb->first();
        if ($row) {
            $num= $row->count + 1;
            $res=$obb->update(['count'=>$num]);
        } else {
            $ob=new Cart();
            $ob->user_id = $uid;
            $ob->product_id = $pid;
            $ob->count = 1;
            $ob->status = 2;
            $res=$ob->save();
        }
        if ($res) {
            $sum=$ob->donum();
            return $sum;
        } else {
            return json_encode(false);
        }
    }
}
